﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaiduFanyi.Config;
using BaiduFanyi.Messages;
using ParkingLot.Utility.MessageHelp;


namespace BaiduFanyi
{
    public partial class BindableForm : Form
    {


        public BindableForm()
        {
            InitializeComponent();
            MessageAggregator<UpdateTypeMessage>.Subscribe(OnUpdateTypeMessage);
            
        }

        public void OnUpdateTypeMessage(UpdateTypeMessage updateTypeMessage)
        {
            this.cmbType.Items.Add(updateTypeMessage.TypaName);
        }

        /// <summary>
        /// 生成可绑定属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuild_Click(object sender, EventArgs e)
        {

            try
            {
                this.txtProm.Clear();
                Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                    return;
                }
                string result = Utilitys.GetNotifyProperty(tuple.Item1, tuple.Item2, tuple.Item3);
                this.txtProm.Text = "内容已经复制到粘贴板";
                Clipboard.SetDataObject(result);//把内容复制到粘贴板

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //string value = GetNotifyProperty(propertyName, txt解释.Text, txtType.Text);

            //Clipboard.SetDataObject(value);//把内容复制到粘贴板
        }

        /// <summary>
        /// 生成VM
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuildVM_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtProm.Clear();
                Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                    return;
                }
                string result = Utilitys.GetAvaloniaProperty(tuple.Item1, tuple.Item2, tuple.Item3);
                this.txtProm.Text = "内容已经复制到粘贴板";
                Clipboard.SetDataObject(result);//把内容复制到粘贴板
                //this.Cursor
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn生成AV依赖属性_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtProm.Clear();
                Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                    return;
                }
                string result = Utilitys.GetAvaloniaStyleProperty(tuple.Item1, tuple.Item2, tuple.Item3,txt父类.Text);
                this.txtProm.Text = "内容已经复制到粘贴板";
                Clipboard.SetDataObject(result);//把内容复制到粘贴板

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn生成附加属性_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtProm.Clear();
                Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                    return;
                }
                string result = Utilitys.GetAttachedProperty(tuple.Item1, tuple.Item2, tuple.Item3, txt父类.Text);
                this.txtProm.Text = "内容已经复制到粘贴板";
                Clipboard.SetDataObject(result);//把内容复制到粘贴板

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BindableForm_Load(object sender, EventArgs e)
        {
            foreach (string s in ProjectConfig.Instance.LisCommonClass)
            {
                ProjectConfig.Instance.LisCommonUseType.Add(new CommonUseType(s));
            }
         
            ProjectConfig.Instance.LisCommonClass.Clear();
            ProjectConfig.Instance.Save();
            Assembly assembly = Assembly.GetAssembly(typeof(string));
            Type[] types = assembly.GetTypes();
            
            string[] data = ProjectConfig.Instance.LisCommonClass.ToArray();
            cmbType.Items.Clear();
            cmbType.Items.AddRange(data);
            //cmbType.TextUpdate += (a, b) =>
            //{
            //    var input = cmbType.Text.ToUpper();
            //    cmbType.Items.Clear();
            //    if (string.IsNullOrWhiteSpace(input))
            //    {
            //        cmbType.Items.AddRange(data);
            //    }
            //    else
            //    {
            //        var newList = data.Where(x => x.IndexOf(input, StringComparison.CurrentCultureIgnoreCase) != -1).ToArray();
            //        cmbType.Items.AddRange(newList);
            //    }
            //    cmbType.Select(cmbType.Text.Length, 0);
            //    cmbType.DroppedDown = true;
            //    //保持鼠标指针形状  
            //    Cursor = Cursors.Default;
            //};
            ;
        }
     
        private void cmbType_TextUpdate(object sender, EventArgs e)
        {
            try
            {
                this.cmbType.Items.Clear();
                //清空listNew
                //遍历全部备查数据(进行智能排序)
                CommonUseTypeCompare commonUseTypeCompare = new CommonUseTypeCompare();
                List<CommonUseType> lisCommonUseTypes = new List<CommonUseType>();
                foreach (KeyValuePair<string, int> keyValuePair in ProjectConfig.Instance.DictCommonClass)
                {
                    if (keyValuePair.Key.Contains(this.cmbType.Text))
                    {
                        CommonUseType commonUseType = new CommonUseType(keyValuePair);
                        if (this.cmbType.Text == keyValuePair.Key)
                        {
                            commonUseType.UseTimes += 100000;
                        }
                        lisCommonUseTypes.Add(commonUseType);
                    }
                }

                lisCommonUseTypes.Sort(commonUseTypeCompare);
                lisCommonUseTypes.Reverse();
                object[] sources;
                if (lisCommonUseTypes.Count>0)
                {
                    sources = lisCommonUseTypes.Select(x => x.Name).ToArray();
                }
                else
                {
                     sources = ProjectConfig.Instance.DictCommonClass.Keys.ToArray();
                }
               





                //foreach (var item in listOnit)
                //{
                //    if (item.Contains(this.cmbType.Text))
                //    {
                //        //符合，插入ListNew
                //        listNew.Add(item);
                //    }
                //}
                //combobox添加已经查到的关键词
                this.cmbType.Items.AddRange(sources);
                //设置光标位置，否则光标位置始终保持在第一列，造成输入关键词的倒序排列
                this.cmbType.SelectionStart = this.cmbType.Text.Length;
                //保持鼠标指针原来状态，有时候鼠标指针会被下拉框覆盖，所以要进行一次设置。
                Cursor = Cursors.Default;
                //自动弹出下拉框
                this.cmbType.DroppedDown = true;
            }
            catch (Exception a)
            {
               
            }
            //原文路径 https://www.cnblogs.com/xilipu31/p/3993049.html
            //清空combobox
          
        }

        private void 取色_Click(object sender, EventArgs e)
        {
            this.txtProm.Clear();
            colorDialog1.ShowDialog();
            StringBuilder sbColor = new StringBuilder();
            sbColor.AppendLine(colorDialog1.Color.Name);
            sbColor.AppendLine(colorDialog1.Color.ToString());
           
            this.txtProm.Text = sbColor.ToString();
            string value = $"#{colorDialog1.Color.Name.ToUpper()}";
            if (colorDialog1.Color.IsKnownColor)
            {
                value = colorDialog1.Color.Name;
            }

            string code=$"<Border Classes=\"BaseColor\" Background=\"{value}\"/>";
            Clipboard.SetDataObject(code);//把内容复制到粘贴板

        }

        private void btn生成WPF属性_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtProm.Clear();
                Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                    return;
                }
                string result = Utilitys.GetWPFNotifyProperty(tuple.Item1, tuple.Item2, tuple.Item3);
                this.txtProm.Text = "内容已经复制到粘贴板";
                Clipboard.SetDataObject(result);//把内容复制到粘贴板

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn生成WPF命令_Click(object sender, EventArgs e)
        {
            this.txtProm.Clear();
            Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
            if (!string.IsNullOrEmpty(error))
            {
                MessageBox.Show(error);
                return;
            }
            string result = Utilitys.GetWPFCommand(tuple.Item1, tuple.Item2, tuple.Item3);
            this.txtProm.Text = result;
        }

        private void btn生成WPF依赖属性_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtProm.Clear();
                Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                    return;
                }
                string result = Utilitys.GetWPFDependencyProperty(tuple.Item1, tuple.Item2, tuple.Item3, txt父类.Text);
                this.txtProm.Text = "内容已经复制到粘贴板";
                Clipboard.SetDataObject(result);//把内容复制到粘贴板

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn生成WPF附加属性_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtProm.Clear();
                Tuple<string, string, string> tuple = Utilitys.GetTuple(txt属性名称.Text, txt注释.Text, cmbType.Text, out string error);
                if (!string.IsNullOrEmpty(error))
                {
                    MessageBox.Show(error);
                    return;
                }
                string result = Utilitys.GetWPFAttachedProperty(tuple.Item1, tuple.Item2, tuple.Item3, txt父类.Text);
                this.txtProm.Text = "内容已经复制到粘贴板";
                Clipboard.SetDataObject(result);//把内容复制到粘贴板

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
